package com.yazi.mxz.person.controller.firm;

import java.util.List;

import javax.annotation.Resource;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.beans.StoreMember;
import com.yazi.mxz.common.entity.beans.StoreMemberId;
import com.yazi.mxz.common.service.MemberService;
import com.yazi.mxz.common.service.StoreMemberService;
import com.yazi.mxz.common.service.StoreService;
import com.yazi.mxz.person.jackson.filter.MemberFilter;

@Controller("firm-storeMemberController")
@RequestMapping("/firm/storeMember")
public class StoreMemberController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(StoreMemberController.class);
	
	@Resource
	MemberService memberService;
	@Resource
	StoreMemberService storeMemberService;
	@Resource
	StoreService storeService;
	
	
	@RequestMapping("/addDo/json")
	@ResponseBody
	public ObjectResult<?> addDoJson(long storeId, String userName, Long roleId) {
		debug(log, "添加店铺管理员");
		ObjectResult<Member> jsonResult = new ObjectResult<Member>();
		jsonResult.setFilter(MemberFilter.get());
		try{
			Member member = new Member();
			member.setName(userName);
			List<Member> mlist = memberService.list(member);
			if (mlist.size() != 1) {
				jsonResult.setErrorMessage("用户不存在");
				return jsonResult;
			}
			
			member = mlist.get(0);
			StoreMemberId storeMemberId = new StoreMemberId();
			storeMemberId.setMemberId(member.getMemberId());
			StoreMember storeMember = new StoreMember();
			storeMember.setId(storeMemberId);
			List<StoreMember> smList = storeMemberService.list(storeMember);
			if (smList.size() != 0) {
				jsonResult.setErrorMessage("该用户已是店铺管理员");
				return jsonResult;
			} 
			
			storeMemberId.setStoreId(storeId);
			storeMember.setId(storeMemberId);
			storeMemberService.addByCompany(getCompany(true), storeMember, roleId);
			jsonResult.setResult(member);
			jsonResult.setOkMessage("添加管理员成功");
		} catch (MyException e) {
			debug(log, "添加店铺管理员失败：" + e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "添加店铺管理员异常");
			jsonResult.setErrorMessage("添加失败，请联系管理员");
		}
		return jsonResult;
	}
	
	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult delDoJson(long storeId, long memberId) {
		debug(log, "删除店铺管理员");
		StatusResult jsonResult = new StatusResult();
		try{
			Store store = storeService.one(storeId);
			// 检查店铺是否属于企业
			if (null != store && store.getCompany().equals(getCompany(true))) {
				StoreMemberId smId = new StoreMemberId();
				smId.setStoreId(storeId);
				smId.setMemberId(memberId);
				StoreMember storeMember = storeMemberService.one(smId);
				if (null != storeMember) {
					storeMemberService.remove(storeMember);
				}
			}
			jsonResult.setOk();
		} catch (MyException e) {
			debug(log, e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e ) {
			error(log, e, "删除店铺管理员异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
}
